// pages/me_content/aplay.js
import {
  uploadFile,
  addReceiver,
  getReceiverInfo,
  getFeeRate,
} from "../api/index";
import { store } from "../../store/index";

Page({
  data: {
    services: [
      // { service: '服务项', fee: '手续费' },
      // { service: '邻里互助', fee: '20%' },
      // { service: '通用服务', fee: '20%' },
      // { service: '游戏开黑', fee: '20%' },
      // { service: '商品代买', fee: '100%' },
      // { service: '保洁上门', fee: '100%' },
      // { service: '家电清洗', fee: '100%' },
      // { service: '代扔垃圾', fee: '100%' },
      // { service: '晾被子', fee: '100%' },
    ],
    formData: {
      imgFront: "",
      idCard: "",
      name: "",
      imgBack: "",
    },
    fileList: {
      imgFront: "",
      imgBack: "",
    },
    step: 0,
  },

  onLoad() {
    this.getFeeRate();
    // 页面加载时的逻辑
    if (store.userInfo?.applyStatus === "驳回") {
      this.getReceiverInfo();
      // this.setData({
      //   step: 1
      // })
    }
  },
  getReceiverInfo() {
    getReceiverInfo().then((res) => {
      console.log(res, "res");
      this.setData({
        formData: {
          name: res.result.name,
          idCard: res.result.idCard,
          id: res.result.id,
          imgFront: res.result.imgFront,
          imgBack: res.result.imgBack,
        },
      });
    });
  },
  handleInput(e) {
    const type = e.currentTarget.dataset.type;
    this.setData({
      [`formData.${type}`]: e.detail.value,
    });
  },
  getFeeRate() {
    getFeeRate().then((res) => {
      console.log(res, "res");
      this.setData({
        services: [
          { service: "服务项", fee: "手续费" },
          ...res.result.map((item, index) => ({
            service: item.name,
            fee: item.fee + "%",
            index,
          })),
        ],
      });
    });
  },

  nextStep() {
    // 处理下一步逻辑
    this.setData({
      step: 1,
    });
  },
  uploadImage(e) {
    wx.chooseImage({
      count: 1,
      sizeType: ["compressed"],
      sourceType: ["album", "camera"],
      success: (res) => {
        console.log(res);
        const type = e.currentTarget.dataset.type;
        this.setData({
          [`formData.${type}`]: res.tempFilePaths[0],
        });
        uploadFile({
          file: res.tempFilePaths[0],
        }).then((res) => {
          this.setData({
            [`fileList.${type}`]: res,
          });
        });
      },
    });
  },
  submit() {
    if (
      !this.data.formData.name ||
      !this.data.formData.idCard ||
      !this.data.formData.imgFront ||
      !this.data.formData.imgBack
    ) {
      wx.showToast({
        title: "请填写完整信息",
        icon: "none",
      });
      return;
    }
    addReceiver({
      ...this.data.formData,
      imgFront: this.data.fileList.imgFront,
      imgBack: this.data.fileList.imgBack,
    }).then((res) => {
      wx.showToast({
        title: "申请提交成功，请等待审核",
        icon: "success",
      });
      wx.navigateBack();
      console.log(res);
    });
  },
});
